package com.feishi.core.util.common;


import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: caixq
 * Date: 2018-10-12
 * Time: 上午10:34
 */
@SuppressWarnings("unchecked")
public class ListUtil {
    /**
     *  for循环
     * @param list
     * @param <T>
     */
    public static <T> void listFor(List<T> list, Consumer<? super T> callBack) {
        list.forEach(callBack);
    }
    /**
     * 基本数据类型去重
     *
     * @param list
     * @param <T>
     * @return
     */
    public static <T> List<T> distinct(List<T> list) {
        return list.stream().distinct().collect(Collectors.toList());
    }

    /**
     * 按对象属性去重
     *
     * @return
     */
    public static <T> List<T> distinct(List<T> list, Comparator<? super T> compare) {

        // 2.多属性去重：把多个属性拼起来( 本质上还是单属性去重 )
        return list.stream().collect(Collectors.collectingAndThen(
                Collectors.toCollection(() -> new TreeSet<>(compare)),
                ArrayList::new));
    }

    /**
     * 过滤
     *
     * @return
     */
    public static <T> List<T> filter(List<T> list, Predicate<? super T> filterFun) {
        return list.stream().filter(filterFun).collect(Collectors.toList());
    }
    public static void main(String[] args){
      List list=new ArrayList();
      int num= (int) 1e3;
        List<String> strList = new ArrayList<String>(){
            {add("我");
            add("你");
            add("他");
            add("他");
            add("他");}
        };


        for (int i = 0; i < num; i++) {
            list.add(Math.random());
        }
        List a = filter(list, (t) -> (double) t < 0.5);
//        System.out.println(a);
        System.out.println(a.size()*1.0/num);
        System.out.println(distinct(strList));
    }
}
